package com.atguigu.gmallre.mock.factory;


import com.atguigu.gmallre.mock.bean.BaseProvince;
import com.atguigu.gmallre.mock.bean.CartInfo;
import com.atguigu.gmallre.mock.bean.FavorInfo;
import com.atguigu.gmallre.mock.bean.UserInfo;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.log.AppCommon;
import com.atguigu.gmallre.mock.service.*;
import com.atguigu.gmallre.mock.stage.Stage;
import com.atguigu.mock.util.RandomBox;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("userStageChain")
@Scope("prototype")
@Data
public class UserStageChain implements  Runnable{




    @Autowired
    UserInfoService userInfoService;

    @Autowired
    BaseProvinceService baseProvinceService;

    @Autowired
    OrderInfoService orderInfoService;


    @Autowired
    CartInfoService cartInfoService;

    @Autowired
    FavorInfoService favorInfoService;


    @Setter
    List<Stage> visitStages =new LinkedList<>();



    @Setter
    Date  startDatetime;





    public UserStageChain addStage(Stage stage){
        visitStages.add(stage);
        return  this;
    }



//    AppCommon common;

    public void initAppCommonInfo(){
        AppCommon appCommon =   AppCommon.build() ;
        if( UserSession.getUserInfo()!=null){
            appCommon.setUid( UserSession.getUserInfo().getId().toString());
        }

        BaseProvince baseProvince = baseProvinceService.getOneByRandom();
        appCommon.setAr(baseProvince.getId().toString());

        appCommon.setSid(UserSession.getSessionId());
        UserSession.setAppCommon(appCommon);
    }

    public void initSessionId(){
        String sessionId = UUID.randomUUID().toString();
        UserSession.setSessionId(sessionId);
    }


    public void   initUserInfo(){
        RandomBox<Boolean> ifloginBox = new RandomBox<>(100 -AppConfig.if_nologin_rate,  AppConfig.if_nologin_rate);
        UserInfo userInfo = null;
        if(ifloginBox.getRandBoolValue()){
            userInfo = userInfoService.getOneByRandom();
            UserSession.setUserInfo(userInfo);

            List<CartInfo> cartInfoList  = cartInfoService.list(new QueryWrapper<CartInfo>().eq("user_id",userInfo.getId()));
            UserSession.setCartList(cartInfoList);

            List<FavorInfo> favorInfoList = favorInfoService.list(new QueryWrapper<FavorInfo>().eq("user_id",userInfo.getId()));
            UserSession.setFavorList(favorInfoList);
        }



    }

    public  void initTime(){
        UserSession.setCurDateTime(startDatetime);
    }




    @Override
    public void run() {
        initSessionId();
        initUserInfo();

        initTime();
        initAppCommonInfo();
        handleStages(visitStages);


    }


    private  void handleStages( List<? extends  Stage> stages){
        for (int i = 0; i < stages.size(); i++) {
            if(i<stages.size()-1){
                UserSession.setNextStageInfo( stages.get(i+1).getClass()  );
            }
            boolean canContinue =  stages.get(i).handle();
            if(!canContinue){
                break;
            }
        }
    }





}
